﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using NCT.Framework;
using NCT.Framework.Database;

namespace VISS.API.Commons {
    class PropertyBC : IProperty, IPropertyAdmin {

        #region IPropertyAdmin Members

        bool IPropertyAdmin.Insert(PropertyEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Normalize data
                item.Name = StringUtils.Normalize(item.Name);

                if (item.DateCreated == DateTimeUtils.MinValue) {
                    item.DateCreated = DateTime.Now;
                }
                item.DateModified = DateTime.Now;

                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                result = PropertyDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.PropertyBC.Insert({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        bool IPropertyAdmin.Update(PropertyEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Normalize data
                item.Name = StringUtils.Normalize(item.Name);

                item.DateModified = DateTime.Now;

                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                result = PropertyDA.Update(item, connectionName, dbCon, dbTrans) > 0;

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.PropertyBC.Update({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();

                }
            }
            return result;
        }

        #endregion

        #region IProperty Members

        public int CountByCategoryID(short CategoryID) {
            try {
                return PropertyDA.CountByPropertyCategoryID(CategoryID, ConfigInfo.ConnectionName);
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.PropertyBC.CountByCategoryID({0}): \n" + ex.ToString(),
                                    CategoryID));
                return 0;
            }
        }

        public List<PropertyEnt> GetByCategoryIDPaging(short categoryID, int page, int recordPerPage, out int totalRecord) {
            totalRecord = 0;
            List<PropertyEnt> result = new List<PropertyEnt>();
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                totalRecord = PropertyDA.CountByPropertyCategoryID(categoryID, connectionName);
                if (totalRecord == 0) {
                    return result;
                }

                int totalPage = (int)(totalRecord / recordPerPage);
                if (totalRecord % recordPerPage != 0) {
                    totalPage++;
                }
                if (page > totalPage) {
                    return result;
                }

                long startRow = (page - 1) * recordPerPage + 1;
                long endRow = startRow + recordPerPage - 1;

                if (endRow > totalRecord) {
                    endRow = totalRecord;
                }

                result = PropertyDA.GetByPropertyCategoryIDWithPaging(categoryID, startRow, endRow, connectionName, null, null);

            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.PropertyBC.GetByCategoryIDPaging({0}, {1}, {2}): \n" + ex.ToString(),
                                    categoryID, page, recordPerPage));
            }
            return result;
        }

        public PropertyEnt GetByID(int iD) {
            try {
                return PropertyDA.GetByID(iD, ConfigInfo.ConnectionName, null, null);
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Commons.PropertyBC.GetByID({0}): \n" + ex.ToString(),
                                   iD));
                return null;
            }
        }

        #endregion
    }
}
